<template>
  <div class="ai-floating-container">
    <!-- 浮动AI按钮 -->
    <transition name="bounce">
      <div 
        v-show="showButton"
        class="ai-floating-button"
        @click="toggleDialog"
        :class="{ 'active': dialogVisible }"
      >
        <div class="ai-icon">
          <span class="moon-icon">🌙</span>
        </div>
        <div class="ai-text">AI助手</div>
      </div>
    </transition>

    <!-- AI对话弹窗 -->
    <el-dialog
      v-model="dialogVisible"
      title=""
      width="900px"
      :show-close="false"
      :modal="true"
      :close-on-click-modal="true"
      :close-on-press-escape="true"
      :resize="true"
      :draggable="true"
      class="ai-chat-dialog"
      @close="handleDialogClose"
    >
      <div class="dialog-content">
        <!-- 自定义关闭按钮 -->
        <button class="custom-close-btn" @click="dialogVisible = false">
          ×
        </button>
        <MoonshotChat />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { ref, onMounted, onUnmounted } from 'vue'
import MoonshotChat from './MoonshotChat.vue'

export default {
  name: 'AiFloatingButton',
  components: {
    MoonshotChat
  },
  setup() {
    const dialogVisible = ref(false)
    const showButton = ref(true)
    let scrollTimer = null

    // 切换对话框
    const toggleDialog = () => {
      dialogVisible.value = !dialogVisible.value
    }

    // 关闭对话框
    const handleDialogClose = () => {
      dialogVisible.value = false
    }

    // 滚动时隐藏按钮，停止滚动时显示
    const handleScroll = () => {
      showButton.value = false
      
      clearTimeout(scrollTimer)
      scrollTimer = setTimeout(() => {
        showButton.value = true
      }, 1000)
    }

    onMounted(() => {
      // 监听滚动事件
      window.addEventListener('scroll', handleScroll, { passive: true })
    })

    onUnmounted(() => {
      window.removeEventListener('scroll', handleScroll)
      if (scrollTimer) {
        clearTimeout(scrollTimer)
      }
    })

    return {
      dialogVisible,
      showButton,
      toggleDialog,
      handleDialogClose
    }
  }
}
</script>

<style scoped>
.ai-floating-container {
  position: fixed;
  bottom: 30px;
  right: 30px;
  z-index: 2000;
}

.ai-floating-button {
  position: relative;
  width: 70px;
  height: 70px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 50%;
  cursor: pointer;
  box-shadow: 0 4px 20px rgba(102, 126, 234, 0.4);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  user-select: none;
  overflow: hidden;
}

.ai-floating-button:hover {
  transform: translateY(-3px) scale(1.05);
  box-shadow: 0 8px 30px rgba(102, 126, 234, 0.6);
}

.ai-floating-button.active {
  background: linear-gradient(135deg, #764ba2 0%, #667eea 100%);
  transform: scale(0.95);
}

.ai-icon {
  font-size: 24px;
  margin-bottom: 2px;
}

.moon-icon {
  display: inline-block;
  animation: float 3s ease-in-out infinite;
}

.ai-text {
  font-size: 10px;
  color: white;
  font-weight: 500;
  opacity: 0.9;
}

/* 浮动动画 */
@keyframes float {
  0%, 100% { transform: translateY(0px); }
  50% { transform: translateY(-3px); }
}

/* 按钮出现动画 */
.bounce-enter-active {
  animation: bounceIn 0.5s ease-out;
}

.bounce-leave-active {
  animation: bounceOut 0.3s ease-in;
}

@keyframes bounceIn {
  0% {
    opacity: 0;
    transform: scale(0.3) translateY(20px);
  }
  50% {
    opacity: 1;
    transform: scale(1.1);
  }
  100% {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

@keyframes bounceOut {
  0% {
    opacity: 1;
    transform: scale(1);
  }
  100% {
    opacity: 0;
    transform: scale(0.3) translateY(20px);
  }
}

/* 对话框样式 - 固定尺寸不自动调整 */
.ai-chat-dialog {
  --el-dialog-border-radius: 20px;
}

.ai-chat-dialog :deep(.el-dialog) {
  resize: both !important;
}

.ai-chat-dialog :deep(.el-dialog.is-fullscreen) {
  width: 900px !important;
  height: 650px !important;
}

.ai-chat-dialog :deep(.el-dialog__wrapper) {
  padding: 0 !important;
  margin: 0 !important;
}

.ai-chat-dialog :deep(.el-dialog) {
  border-radius: 20px !important;
  overflow: hidden !important;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15) !important;
  border: none !important;
  backdrop-filter: blur(10px) !important;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
  margin: 0 !important;
  padding: 0 !important;
  position: relative !important;
  width: 900px !important;
  height: 650px !important;
  min-width: 400px !important;
  min-height: 300px !important;
  max-width: 90vw !important;
  max-height: 90vh !important;
  resize: both !important;
}

.ai-chat-dialog :deep(.el-overlay-dialog) {
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
  padding: 0 !important;
  margin: 0 !important;
}

.ai-chat-dialog :deep(.el-dialog__header) {
  display: none !important;
}

.ai-chat-dialog :deep(.el-dialog__headerbtn) {
  display: none !important;
}

.ai-chat-dialog :deep(.el-dialog__body) {
  padding: 0 !important;
  height: 650px !important;
  max-height: 650px !important;
  min-height: 300px !important;
  overflow: hidden !important;
  margin: 0 !important;
  border-radius: 20px;
  position: relative;
  box-sizing: border-box !important;
}

.ai-chat-dialog :deep(.el-dialog__body::before) {
  content: '';
  position: absolute;
  top: -10px;
  left: -10px;
  right: -10px;
  bottom: -10px;
  background: transparent;
  z-index: -1;
}

.ai-chat-dialog :deep(.el-dialog__title) {
  display: none !important;
}

.dialog-content {
  height: 650px !important;
  max-height: 650px !important;
  min-height: 300px;
  background: white;
  position: relative;
  border-radius: 20px;
  overflow: hidden;
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
}

/* 优化内部聊天组件样式 */
.dialog-content :deep(.moonshot-chat) {
  height: 650px !important;
  max-height: 650px !important;
  border: none;
  border-radius: 0;
  box-shadow: none;
  background: transparent;
  display: flex;
  flex-direction: column;
  overflow: hidden !important;
}

.dialog-content :deep(.chat-header) {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
  border-radius: 20px 20px 0 0 !important;
  padding: 12px 20px !important;
  margin: 0 !important;
  position: relative !important;
  z-index: 1 !important;
  border: none !important;
  flex-shrink: 0;
  height: 60px !important;
}

.dialog-content :deep(.chat-messages) {
  background: #fafbfc !important;
  padding: 0 !important;
  margin: 0 !important;
  flex: 1;
  overflow-y: auto;
  border: none !important;
  min-height: 300px !important;
}

.dialog-content :deep(.chat-input) {
  background: white !important;
  border-top: 1px solid #e4e7ed !important;
  border-radius: 0 0 20px 20px !important;
  flex-shrink: 0;
  margin: 0 !important;
  padding: 15px 20px !important;
  min-height: 120px !important;
  height: auto !important;
}

.dialog-content :deep(.quick-actions) {
  background: #f8f9fa !important;
  flex-shrink: 0;
  margin: 0 !important;
  padding: 8px 20px !important;
  border-top: 1px solid #e4e7ed !important;
  min-height: 50px !important;
  height: auto !important;
}

/* 修复消息区域内部样式 */
.dialog-content :deep(.welcome-message) {
  padding: 40px 20px !important;
}

.dialog-content :deep(.message) {
  margin: 20px !important;
}

.dialog-content :deep(.message-content) {
  margin: 0 12px !important;
}

/* 优化输入区域内部元素 */
.dialog-content :deep(.chat-input .el-input) {
  margin-bottom: 8px !important;
}

.dialog-content :deep(.chat-input .input-actions) {
  margin-top: 8px !important;
  display: flex !important;
  justify-content: space-between !important;
  align-items: center !important;
}

.dialog-content :deep(.chat-input .action-buttons) {
  display: flex !important;
  gap: 8px !important;
}

.dialog-content :deep(.chat-input .el-textarea) {
  height: 60px !important;
}

.dialog-content :deep(.chat-input .el-textarea__inner) {
  height: 60px !important;
  resize: none !important;
}

/* 自定义关闭按钮 */
.custom-close-btn {
  position: absolute;
  top: 20px;
  right: 20px;
  z-index: 1001;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.9);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  color: #666;
  font-size: 16px;
  font-weight: bold;
  line-height: 1;
}

.custom-close-btn:hover {
  background: rgba(255, 255, 255, 1);
  transform: scale(1.1);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  color: #333;
}

/* 响应式设计 - 固定尺寸 */
@media (max-width: 1200px) {
  .ai-chat-dialog :deep(.el-dialog) {
    width: 750px !important;
    height: 550px !important;
  }
  
  .ai-chat-dialog :deep(.el-dialog__body) {
    height: 550px !important;
    max-height: 550px !important;
  }
  
  .dialog-content {
    height: 550px !important;
    max-height: 550px !important;
  }
  
  .dialog-content :deep(.moonshot-chat) {
    height: 550px !important;
    max-height: 550px !important;
  }
}

@media (max-width: 768px) {
  .ai-floating-container {
    bottom: 20px;
    right: 20px;
  }
  
  .ai-floating-button {
    width: 60px;
    height: 60px;
  }
  
  .ai-icon {
    font-size: 20px;
  }
  
  .ai-text {
    font-size: 9px;
  }
  
  .ai-chat-dialog :deep(.el-dialog) {
    width: 500px !important;
    height: 450px !important;
    border-radius: 16px !important;
  }
  
  .ai-chat-dialog :deep(.el-dialog__body) {
    height: 450px !important;
    max-height: 450px !important;
  }
  
  .dialog-content {
    height: 450px !important;
    max-height: 450px !important;
  }
  
  .dialog-content :deep(.moonshot-chat) {
    height: 450px !important;
    max-height: 450px !important;
  }
}

@media (max-width: 480px) {
  .ai-chat-dialog :deep(.el-dialog) {
    width: 380px !important;
    height: 350px !important;
  }
  
  .ai-chat-dialog :deep(.el-dialog__body) {
    height: 350px !important;
    max-height: 350px !important;
  }
  
  .dialog-content {
    height: 350px !important;
    max-height: 350px !important;
  }
  
  .dialog-content :deep(.moonshot-chat) {
    height: 350px !important;
    max-height: 350px !important;
  }
}

/* 暗色主题适配 */
@media (prefers-color-scheme: dark) {
  .ai-floating-button {
    box-shadow: 0 4px 20px rgba(102, 126, 234, 0.3);
  }
  
  .ai-floating-button:hover {
    box-shadow: 0 8px 30px rgba(102, 126, 234, 0.5);
  }
}
</style>
